package com.wgj.android.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.jiaai.xui.adapter.XRecyclerViewAdapter;
import com.jiaai.xui.adapter.XViewHolder;
import com.jiaai.xui.view.widget.XToast;
import com.wgj.android.R;
import com.wgj.android.entity.Group;
import com.wgj.android.entity.WXGroup;
import com.wgj.android.utils.AnimationUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * ========================================
 * 作    者： 佳爱网络有限公司(wwww.jiaailove.com)
 * 版    本： 1.0
 * 创建日期：  2018/1/2
 * 描    述： 微信群加好友，群选择界面
 * 修订历吏：
 * =========================================
 */
public class WXGroupView extends LinearLayout {
    @BindView(R.id.onStartWarp)
    View onStartView;
    @BindView(R.id.startWarp)
    View startView;

    @BindView(R.id.a1)
    View animView;

    @BindView(R.id.listView)
    RecyclerView groupListView;

    private XRecyclerViewAdapter  recyclerViewAdapter;

    private WXGroupCallback callback;

    private List<WXGroup> groupList=new ArrayList<>();
    private List<WXGroup> selectGroupList=new ArrayList<>();
    public WXGroupView(Context context) {
        this(context,null);
    }
    public WXGroupView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }
    public WXGroupView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view= LayoutInflater.from(context).inflate(R.layout.wx_group_view,this);
        ButterKnife.bind(view);
        init();
    }


    private  void  init(){
        recyclerViewAdapter=new XRecyclerViewAdapter<WXGroup>(groupListView,groupList,R.layout.wx_group_item){
            @Override
            protected void bindData(XViewHolder viewHolder, WXGroup group, int i) {
                    viewHolder.setText(R.id.groupname,group.getGroupName());
                    if(selectGroupList.contains(group)){
                        viewHolder.setImageResource(R.id.checkbox,R.mipmap.icon_white_check_select);
                    }else {
                        viewHolder.setImageResource(R.id.checkbox,R.mipmap.icon_check_no);
                    }
            }
        };
        recyclerViewAdapter.setOnItemClickListener(new XRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int i) {
                WXGroup group=groupList.get(i);
                if(selectGroupList.contains(group)){
                    selectGroupList.remove(group);
                }else {
                    selectGroupList.add(group);
                }
                recyclerViewAdapter.notifyDataSetChanged();
            }
        });
         groupListView.setLayoutManager(new LinearLayoutManager(getContext()));
         groupListView.setHasFixedSize(true);
         groupListView.setAdapter(recyclerViewAdapter);
         animView.startAnimation(AnimationUtil.createRotateAnimation(360,-360,5000));
    }

    public  void  loadGroupEnd(List<WXGroup> groups){
        groupList.addAll(groups);
        recyclerViewAdapter.notifyDataSetChanged();
    }

    @OnClick(R.id.stop)
    public  void  onStopClick(View view){
        if(callback!=null){
            callback.onStop();
        }
    }


    @OnClick(R.id.startBut)
    public  void  onStartClick(View view){
        if(selectGroupList.size()==0){
            XToast.error("请选择微信群");
            return;
        }
        if(callback!=null){
            callback.onStart(selectGroupList);
        }
    }

    public void setCallback(WXGroupCallback callback) {
        this.callback = callback;
    }

    public  interface   WXGroupCallback{
        void  onStop();
        void  onStart(List<WXGroup> selects);
    }
}
